/*                                  ATELIER                                   */

/* %%LICENSE_TAG%%                                                            */
package edu.gmu.view.cube;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * The ControlPanel sets the various parameters for a 3D-coordinate system.
 * @author  James H. Pope
 * @version $Revision:$ $Date:$
 */
public class PanelTranslate extends JPanel
{
    // The object that is interested in the values we produce
    private ControlModel controlModel = null;

    //---------------------  Translation Controls  ---------------------------//
    /**
     * xTranslation
     */
    private JLabel xTranslateLabel    = null;
    private JSpinner xTranslateField  = null;

    /**
     * yTranslation
     */
    private JLabel yTranslateLabel    = null;
    private JSpinner yTranslateField  = null;

    /**
     * zTranslation
     */
    private JLabel zTranslateLabel    = null;
    private JSpinner zTranslateField  = null;

    /**
     * Creates a new instance of ControlPanel with specified values.
     * @param controlModel
     */
    public PanelTranslate( ControlModel pControlModel )
    {
        this.controlModel = pControlModel;
        
        //--------------------  Translation Controls  ------------------------//
        this.xTranslateLabel  = new JLabel( "X: " );
        final SpinnerNumberModel xTranslateModel = new SpinnerNumberModel(0.0, -1000.0, 1000.0, 10.0);
        xTranslateModel.addChangeListener(new ChangeListener()
            {
                public void stateChanged(ChangeEvent ce)
                {
                    double newValue = xTranslateModel.getNumber().doubleValue();
                    controlModel.setXTranslate( newValue );
                }
            }
        );
        this.xTranslateField  = new JSpinner( xTranslateModel );
        

        this.yTranslateLabel  = new JLabel( "Y" );
        final SpinnerNumberModel yTranslateModel = new SpinnerNumberModel(0.0, -1000.0, 1000.0, 10.0);
        yTranslateModel.addChangeListener(new ChangeListener()
            {
                public void stateChanged(ChangeEvent ce)
                {
                    double newValue = yTranslateModel.getNumber().doubleValue();
                    controlModel.setYTranslate( newValue );
                }
            }
        );
        this.yTranslateField  = new JSpinner( yTranslateModel );

        this.zTranslateLabel  = new JLabel( "Z" );
        final SpinnerNumberModel zTranslateModel = new SpinnerNumberModel(0.0, -1000.0, 1000.0, 10.0);
        zTranslateModel.addChangeListener(new ChangeListener()
            {
                public void stateChanged(ChangeEvent ce)
                {
                    double newValue = zTranslateModel.getNumber().doubleValue();
                    controlModel.setZTranslate( newValue );
                }
            }
        );
        this.zTranslateField  = new JSpinner( zTranslateModel );

        
        
        // Add the labels/fields to the panel
        JPanel translationPanel = new JPanel();
        translationPanel.setBorder(BorderFactory.createTitledBorder( "Translate" ));
        BoxLayout translationPanelLayout = new BoxLayout( translationPanel, BoxLayout.Y_AXIS );
        translationPanel.setLayout( translationPanelLayout );

        translationPanel.add( createRow( xTranslateLabel,    xTranslateField ) );
        translationPanel.add( createRow( yTranslateLabel,    yTranslateField ) );
        translationPanel.add( createRow( zTranslateLabel,    zTranslateField ) );
        

        this.setLayout( new BorderLayout() );
        this.add( translationPanel, BorderLayout.CENTER );
    }

    /**
     *
     * @param label
     * @param field
     * @return
     */
    public JPanel createRow( JLabel label, JComponent field )
    {
    	JPanel rowPanel = new JPanel( );
    	Dimension labelDim = new Dimension( 100, 20 );
    	Dimension fieldDim = new Dimension( 150, 20 );

    	label.setPreferredSize( labelDim );
    	field.setPreferredSize( fieldDim );

    	rowPanel.add( label );
    	rowPanel.add( field );

    	return rowPanel;
    }

}

/*                                  ATELIER                                   */